package AdminClient;

import java.io.*;
import java.util.*;
import static AdminClient.Debug.*;

public class Room implements Serializable {
	private String name;
	private String description;
	private ObjectList<Portal> portals;
	private static final String delimiter = Msg_Pipe.delimiter;
	
	public Room(String name, String desc) {
		portals = new ObjectList<Portal>();
		this.name = name;
		this.description = desc;
	}

	public boolean equals(Room r) {
		return(r.name == name && r.description == description);
	}

	public String get_name() {
		return(name);
	}
	
	public void set_name(String name) {
		this.name=name;
	}

	public String get_description() {
		return(description);
	}
	
	public void set_description(String description){
		this.description=description;
	}

	public boolean add_portal(Portal p) {
		return(portals.add(p));
	}

	public boolean remove_portal(Portal p) {
		
		return(portals.remove(p));
	}

	public Portal get_portal(String room) {
		Portal p = null;

		for (int i = 0; i < portals.size(); i++) {
			if (portals.get(i).get_room().equals(room)) {
				p = portals.get(i);
				break;
			}
		}

		return(p);
	}

	public String[] get_portals() {
		String[] ps = new String[portals.size()];
		Portal p;

		for (int i = 0; i < portals.size(); i++) {
			p = portals.get(i);
			ps[i] = p.get_room() + " " + p.get_server() + " " +
				p.get_client_port() + " " + p.get_update_port();
			//p[i] = portals.get(i).get_room() + delimiter + ;
			}
		return(ps);
	}
	public ObjectList<Portal> getPortals(){
		return portals;
	}
	public String toString() {
		/* Changes made here must be made to fromString as well */
		return(name + delimiter + description);
	}

	public static Room fromString(String str) throws InvalidMessageException {
		/* str - a msg received from a Msg_Pipe.recv_message() call
		 * the first field will be the message tupe, followed by the
		 * result of calling Room.toString().  Changes to toString must
		 * be included here as well.
		 */
		String[] result = str.split("\\" + delimiter);
		if (result.length != 2)
			throw(new InvalidMessageException("Invalid Room"));
		return(new Room(result[0], result[1]));
	}
}
